package com.zhanghe.study.java8.stream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 终止操作
 * 查找与匹配
 * allMatch -- 检查是否匹配所有元素
 * anyMatch -- 检查是否至少匹配一个元素
 * noneMatch -- 检查是否没有匹配到元素
 * findFirst -- 返回第一个元素
 * findAny -- 返回当前流中的任意元素
 * count -- 返回流中元素的总个数
 * max -- 返回流中最大值
 * min -- 返回流中最小值
 */
public class TestStreamApi3 {
    private static List<User> list = new ArrayList<>(
            Arrays.asList(
                    new User(1,"张三",18, (byte) 0),
                    new User(2,"李四",20, (byte) 1),
                    new User(3,"王五",16, (byte) 0),
                    new User(4,"赵六",25, (byte) 1),
                    new User(5,"张三",30, (byte) 0),
                    new User(6,"张三",8, (byte) 0)
            )
    );

    public static void main(String[] args) {
        System.out.println("=====testAllMatch==========");
        testAllMatch();
        System.out.println("=====testAnyMatch==========");
        testAnyMatch();
        System.out.println("=====testNoneMatch==========");
        testNoneMatch();
        System.out.println("=====testFindFirst==========");
        testFindFirst();
        System.out.println("=====testFindAny==========");
        testFindAny();
        System.out.println("=====testCount==========");
        testCount();
        System.out.println("=====testMax==========");
        testMax();
        System.out.println("=====testMin==========");
        testMin();
    }

    /**
     * allMatch测试
     */
    public static void testAllMatch(){
        boolean isSex = list.stream().allMatch(
                l -> l.getSex() == 0
        );
        System.out.println(isSex);
    }

    /**
     * anyMatch测试
     */
    public static void testAnyMatch(){
        boolean isSex = list.stream().anyMatch(
                l -> l.getSex() == 0
        );
        System.out.println(isSex);
    }

    /**
     * noneMatch测试
     */
    public static void testNoneMatch(){
        boolean isSex = list.stream().noneMatch(
                l -> l.getSex() == 0
        );
        System.out.println(isSex);
    }

    /**
     * findFirst测试
     */
    public static void testFindFirst(){
        User user = list.stream().sorted(
                ((o1, o2) -> {
                    if(o1.getAge() > o2.getAge()){
                        return -1;
                    } else if(o1.getAge() < o2.getAge()){
                        return 1;
                    }
                    return 0;
                })
        ).findFirst().get();
        System.out.println(user);
    }

    /**
     * findAny测试
     */
    public static void testFindAny(){
        User user = list.stream().filter(
                l -> l.getSex() == 0
        ).findAny().get();
        System.out.println(user);
    }

    /**
     * count测试
     */
    public static void testCount(){
        long count = list.stream().count();
        System.out.println(count);
    }

    /**
     * max测试
     */
    public static void testMax(){
        User user = list.stream().max(
                ((o1, o2) -> {
                    if(o1.getAge() > o2.getAge()){
                        return 1;
                    } else if(o1.getAge() < o2.getAge()){
                        return -1;
                    }
                    return 0;
                })
        ).get();
        System.out.println(user);
    }

    /**
     * min测试
     */
    public static void testMin(){
        User user = list.stream().min(
                ((o1, o2) -> {
                    if(o1.getAge() > o2.getAge()){
                        return 1;
                    } else if(o1.getAge() < o2.getAge()){
                        return -1;
                    }
                    return 0;
                })
        ).get();
        System.out.println(user);
    }

    static class User{
        private long id;
        private String name;
        private int age;
        private byte sex;

        @Override
        public String toString() {
            return "User{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", age=" + age +
                    ", sex=" + sex +
                    '}';
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            User user = (User) o;
            return id == user.id &&
                    age == user.age &&
                    sex == user.sex &&
                    Objects.equals(name, user.name);
        }

        @Override
        public int hashCode() {
            return Objects.hash(id, name, age, sex);
        }

        public User(long id, String name, int age, byte sex){
            this.id = id;
            this.name = name;
            this.age = age;
            this.sex = sex;
        }

        public long getId() {
            return id;
        }

        public void setId(long id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public byte getSex() {
            return sex;
        }

        public void setSex(byte sex) {
            this.sex = sex;
        }
    }
}
